package com.hjwoo.app.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.hjwoo.app.R;
import com.hjwoo.app.adpter.LeadsLVAdapter;
import com.hjwoo.app.been.FailureResultObject;
import com.hjwoo.app.been.OrdersObject;
import com.hjwoo.app.net.RequestListener;
import com.hjwoo.app.net.RequestMethod;
import com.hjwoo.app.net.RequestTag;
import com.hjwoo.app.utils.JsonParser;
import com.hjwoo.app.utils.LoanAccessTokenIdKeeper;
import com.hjwoo.app.utils.LoanLog;
import com.hjwoo.app.utils.LoanToast;
import com.hjwoo.app.views.CommunityActivity;
import com.hjwoo.app.views.MainActivity;
import com.hjwoo.app.views.OrderTrackingActivity;

import java.util.LinkedList;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

public class LeadsFragment extends Fragment implements RequestListener {

    public static final String TAG = "LeadsFragment";
    public static LeadsFragment instance;

    public static RelativeLayout orders_isnull_rl;

    //    判断是否刷新
    public static boolean isRefresh = true;
    public static LeadsLVAdapter leadsLVAdapter1;
    public static LeadsLVAdapter leadsLVAdapter2;
    public static View layoutview;
    public ListView leads_lv, leads2_lv;
    public PtrClassicFrameLayout mPtrFrame, mPtrFrame2;
    public static LinkedList<OrdersObject> orders;
    public static LinkedList<OrdersObject> orders2;
    public int[] imageViewIds;
    public Gson gson = new Gson();
    public LinearLayout neterror;
    public LinearLayout leads_layoutleft, leads_layoutright;
    public static Button leadstitle_left_bt, leadstitle_right_bt;
    public TextView child_orders_show_tv;
    private Button orders_pingfang_btn;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        layoutview = inflater.inflate(R.layout.fragment_leads, container, false);
        instance = LeadsFragment.this;
        initView(layoutview);
        return layoutview;

    }

    void initView(View view) {
        neterror = (LinearLayout) view.findViewById(R.id.neterror_linear);
        neterror.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getOrderList();
            }
        });
        leadstitle_left_bt = (Button) view.findViewById(R.id.leadstitle_left_bt);
        leadstitle_right_bt = (Button) view.findViewById(R.id.leadstitle_right_bt);
        leads_layoutleft = (LinearLayout) view.findViewById(R.id.leads_layoutleft);
        leads_layoutright = (LinearLayout) view.findViewById(R.id.leads_layoutright);

        orders_isnull_rl = (RelativeLayout) view.findViewById(R.id.orders_isnull_rl);
        leads_lv = (ListView) view.findViewById(R.id.leads_lv);
        mPtrFrame = (PtrClassicFrameLayout) view.findViewById(R.id.rotate_header_list_view_frame);
        //获取待预约的控件
        leads2_lv = (ListView) view.findViewById(R.id.leads2_lv);
        mPtrFrame2 = (PtrClassicFrameLayout) view.findViewById(R.id.rotate_header_list_view2_frame);
        child_orders_show_tv = (TextView) view.findViewById(R.id.child_orders_show_tv);
        orders_pingfang_btn = (Button) view.findViewById(R.id.orders_pingfang_btn);
        // 已评房下拉刷新
        mPtrFrame.setLastUpdateTimeRelateObject(this);
        mPtrFrame.setPtrHandler(new PtrHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                //        获取已评房订单列表
                RequestMethod.getMethod().queryByStageForFragment(new String[]{"评房已完成"}, new String[]{"成功"}, LeadsFragment.this.getActivity(), LeadsFragment.this);
            }
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
            }
        });
        // 预约下拉刷新
        mPtrFrame2.setLastUpdateTimeRelateObject(this);
        mPtrFrame2.setPtrHandler(new PtrHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                //        获取已预约订单列表==已初审
                RequestMethod.getMethod().queryByStage(new String[]{"房产初审已完成"}, new String[]{"成功"}, LeadsFragment.this.getActivity(), LeadsFragment.this);
            }

            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
            }
        });
        leads_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent mIntent = new Intent(LeadsFragment.this.getActivity(), OrderTrackingActivity.class);
                Bundle mBundle = new Bundle();
                mBundle.putSerializable("order", orders.get(i));
                mIntent.putExtras(mBundle);
                startActivity(mIntent);

            }
        });
        //待预约的点击事件
        leads2_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent mIntent = new Intent(LeadsFragment.this.getActivity(), OrderTrackingActivity.class);
                Bundle mBundle = new Bundle();
                mBundle.putSerializable("order", orders2.get(i));
                mIntent.putExtras(mBundle);
                startActivity(mIntent);
            }
        });

        orders_pingfang_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!LoanAccessTokenIdKeeper.readAccessToken(LeadsFragment.this.getActivity()).getAppSessionId().equals("")) {
                    startActivity(new Intent(LeadsFragment.this.getActivity(), CommunityActivity.class));
                } else {
                    //请登录
                    MainActivity.currIndex = 3;
                    MainActivity.showFragment();
                    MainActivity.group.check(R.id.main_footbar_user);
                    LoanToast.showToastShort("请登录", LeadsFragment.this.getActivity());
                }
            }
        });
        //已报单
        leadstitle_left_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                leadstitle_right_bt.setBackground(getResources().getDrawable(R.drawable.btn_trans_background));
                leadstitle_right_bt.setTextColor(getResources().getColor(R.color.white));
                leadstitle_left_bt.setBackground(getResources().getDrawable(R.drawable.btn_write_background));
                leadstitle_left_bt.setTextColor(getResources().getColor(R.color.home_boder_color));
                leads_layoutleft.setVisibility(View.VISIBLE);
                leads_layoutright.setVisibility(View.GONE);
                RequestMethod.getMethod().queryByStageForFragment(new String[]{"评房已完成"}, new String[]{"成功"}, LeadsFragment.this.getActivity(), LeadsFragment.this);
                if (orders != null){
                    if (orders.size() == 0) {
                        //将图片设置成显示
                        LeadsFragment.orders_isnull_rl.setVisibility(View.VISIBLE);
                        child_orders_show_tv.setText("您还没有评房记录哦!");
                        orders_pingfang_btn.setVisibility(View.VISIBLE);

                    } else {
                        //将图片设置成隐藏
                        LeadsFragment.orders_isnull_rl.setVisibility(View.GONE);
                    }
                }

            }
        });
        //待预约
        leadstitle_right_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                leadstitle_right_bt.setBackground(getResources().getDrawable(R.drawable.btn_write_background));
                leadstitle_right_bt.setTextColor(getResources().getColor(R.color.home_boder_color));
                leadstitle_left_bt.setBackground(getResources().getDrawable(R.drawable.btn_trans_background));
                leadstitle_left_bt.setTextColor(getResources().getColor(R.color.white));
                leads_layoutleft.setVisibility(View.GONE);
                leads_layoutright.setVisibility(View.VISIBLE);
                RequestMethod.getMethod().queryByStage(new String[]{"房产初审已完成"}, new String[]{"成功"}, LeadsFragment.this.getActivity(), LeadsFragment.this);
               if (orders2 != null) {
                   if (orders2.size() == 0) {
                       //将图片设置成显示
                       LeadsFragment.orders_isnull_rl.setVisibility(View.VISIBLE);
                       child_orders_show_tv.setText("您还没有待预约的订单哦!");
                       orders_pingfang_btn.setVisibility(View.GONE);
                   } else {
                       //将图片设置成隐藏
                       LeadsFragment.orders_isnull_rl.setVisibility(View.GONE);
                   }
               }
            }
        });
        //获取订单列表
        getOrderList();
    }

    @Override
    public void requestSuccess(String tag, String message) {
        switch (tag) {
            case RequestTag.QUERYBYSTAGEFORFRAGMENT://已报单
                orders = JsonParser.getJsonParser().parserJosnToList(message);
                LoanLog.e("orders", orders.size() + ";" + orders.toString());
                if (orders.size() == 0) {
                    //将图片设置成显示
                    LeadsFragment.orders_isnull_rl.setVisibility(View.VISIBLE);
                    child_orders_show_tv.setText("您还没有评房记录哦!");
                    orders_pingfang_btn.setVisibility(View.VISIBLE);
                } else {
                    //将图片设置成隐藏
                    LeadsFragment.orders_isnull_rl.setVisibility(View.GONE);
                }
                leadsLVAdapter1 = new LeadsLVAdapter(LeadsFragment.this.getActivity(), orders);
                leads_lv.setAdapter(leadsLVAdapter1);
                mPtrFrame.refreshComplete();
                mPtrFrame.setVisibility(View.VISIBLE);
                if (LeadsFragment.instance.leads_layoutleft.getVisibility() == View.VISIBLE) {
                    if (LeadsFragment.orders != null && LeadsFragment.orders.size() == 0) {
                        //将图片设置成显示
                        LeadsFragment.orders_isnull_rl.setVisibility(View.VISIBLE);
                        child_orders_show_tv.setText("您还没有评房记录哦!");
                        orders_pingfang_btn.setVisibility(View.VISIBLE);
                    } else {
                        //将图片设置成隐藏
                        LeadsFragment.orders_isnull_rl.setVisibility(View.GONE);
                    }
                } else if (LeadsFragment.instance.leads_layoutright.getVisibility() == View.VISIBLE) {
                    if (LeadsFragment.orders2 != null && LeadsFragment.orders2.size() == 0) {
                        //将图片设置成显示
                        LeadsFragment.orders_isnull_rl.setVisibility(View.VISIBLE);
                        child_orders_show_tv.setText("您还没有待预约的订单哦!");
                        orders_pingfang_btn.setVisibility(View.GONE);
                    } else {
                        //将图片设置成隐藏
                        LeadsFragment.orders_isnull_rl.setVisibility(View.GONE);
                    }
                }
                break;
            case RequestTag.QUERYBYSTAGE://待预约列表
                orders2 = JsonParser.getJsonParser().parserJosnToList(message);
                LoanLog.e("orders2", orders2.size() + ";" + orders2.toString());
                if (orders2.size() == 0) {
                    //将图片设置成显示
                    LeadsFragment.orders_isnull_rl.setVisibility(View.VISIBLE);
                    child_orders_show_tv.setText("您还没有待预约的订单哦!");
                    orders_pingfang_btn.setVisibility(View.GONE);
                } else {
                    //将图片设置成隐藏
                    LeadsFragment.orders_isnull_rl.setVisibility(View.GONE);
                }
                leadsLVAdapter2 = new LeadsLVAdapter(LeadsFragment.this.getActivity(), orders2);
                leads2_lv.setAdapter(leadsLVAdapter2);
                mPtrFrame2.refreshComplete();
                mPtrFrame2.setVisibility(View.VISIBLE);
                break;
            case RequestTag.APPCREATE:
                break;
        }
        neterror.setVisibility(View.GONE);
    }

    @Override
    public void requestError(String message) {
        mPtrFrame.refreshComplete();
        orders.removeAll(orders);
        leadsLVAdapter1 = new LeadsLVAdapter(LeadsFragment.this.getActivity(), orders);
        leads_lv.setAdapter(leadsLVAdapter1);
        if (orders.size() == 0) {
            //将图片设置成显示
            LeadsFragment.orders_isnull_rl.setVisibility(View.VISIBLE);
            child_orders_show_tv.setText("您还没有评房记录哦!");
            orders_pingfang_btn.setVisibility(View.VISIBLE);
        } else {
            //将图片设置成隐藏
            LeadsFragment.orders_isnull_rl.setVisibility(View.GONE);
        }
        FailureResultObject failureResultObject = new FailureResultObject();
        failureResultObject = gson.fromJson(message, FailureResultObject.class);
        LoanToast.showToastShort(failureResultObject.getErrorMessage(), LeadsFragment.this.getActivity());
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            // 不在最前端界面显示
//            LoanLog.e(TAG, "yicang");
        } else {// 重新显示到最前端中
//            LoanLog.e(TAG, "xianshi");
            //刷新列表
//            getOrderList();
        }
    }
    /**
     * 获取订单列表
     */
    public void getOrderList() {
        if (LeadsFragment.instance.leads_layoutleft.getVisibility() == View.VISIBLE) {
            // 获取待报单列表
            RequestMethod.getMethod().queryByStageForFragment(new String[]{"评房已完成"}, new String[]{"成功"}, LeadsFragment.this.getActivity(), this);
        } else if (LeadsFragment.instance.leads_layoutright.getVisibility() == View.VISIBLE) {
            //获取待预约的数据
            RequestMethod.getMethod().queryByStage(new String[]{"房产初审已完成"}, new String[]{"成功"}, LeadsFragment.this.getActivity(), LeadsFragment.this);
        }
    }
}